package com.smart.community.feign.property.fallback;

import java.util.Collections;
import java.util.List;

import org.springframework.stereotype.Component;

import com.smart.community.feign.common.BaseFeignFallbackFactory;
import com.smart.community.feign.property.service.IPropertyCompanyCommunityFeign;

import lombok.extern.slf4j.Slf4j;

/**
 * 物业公司社区关联Feign降级处理类
 * 
 * 当物业公司社区关联服务不可用时，提供降级处理：
 * 1. 返回安全的默认值，避免调用方异常
 * 2. 记录详细的错误日志，便于问题排查
 * 3. 确保系统稳定性，避免级联故障
 * 
 * @author Wu.Liang
 * @version 2.0.0
 * @since 2025-06-29
 * @updated 2025-08-28 - 继承BaseFeignFallbackFactory，使用统一的降级处理标准
 */
@Slf4j
@Component
public class PropertyCompanyCommunityFeignFallback extends BaseFeignFallbackFactory<IPropertyCompanyCommunityFeign> {
	@Override
	public IPropertyCompanyCommunityFeign create(Throwable cause) {
		// 使用基类的统一日志记录方法，区分异常类型
		if (isServiceUnavailable(cause)) {
			logServiceUnavailable(getServiceName(), cause);
		} else {
			log.warn("{}服务调用异常，执行降级处理: {}", getServiceName(), cause.getMessage());
		}

		return new IPropertyCompanyCommunityFeign() {

			@Override
			public void assignCommunities(Long propertyCompanyId, List<Long> communityIds) {
				logMethodFallback("assignCommunities", propertyCompanyId, communityIds);
				// 降级处理：静默失败，记录日志
			}

			@Override
			public List<Long> getCommunityIdsByPropertyCompanyId(Long propertyCompanyId) {
				logMethodFallback("getCommunityIdsByPropertyCompanyId", propertyCompanyId);
				return Collections.emptyList(); // 返回空列表，不影响业务
			}

			@Override
			public Boolean checkPropertyCompanyHasCommunity(Long propertyCompanyId, Long communityId) {
				logMethodFallback("checkPropertyCompanyHasCommunity", propertyCompanyId, communityId);
				return Boolean.FALSE; // 返回false，确保安全性
			}

			@Override
			public Long getPropertyCompanyIdByCommunityId(Long communityId) {
				logMethodFallback("getPropertyCompanyIdByCommunityId", communityId);
				return null; // 返回null，表示未关联任何物业公司
			}

			@Override
			public void removeCommunityRelation(Long id) {
				logMethodFallback("removeCommunityRelation", id);
				// 降级处理：静默失败，记录日志
			}

			@Override
			public void removeCommunityRelations(Long propertyCompanyId) {
				logMethodFallback("removeCommunityRelations", propertyCompanyId);
				// 降级处理：静默失败，记录日志
			}

		};
	}
	
	@Override
	protected String getServiceName() {
		return "物业公司社区关联服务";
	}
}